package com.white.hot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

import com.white.hot.constant.ConstantInfo;
import com.white.hot.dto.resp.BaseResp;
import com.white.hot.utils.MD5Util;


public class BaseController
{
	public BaseResp buildParamIllegalResp(BaseResp br)
	{
		br.setStatus(ConstantInfo.RSC_PARAM_ILLEGAL);
		br.setInfo(ConstantInfo.RSS_PARAM_ILLEGAL);
		return br;
	}
	
	public BaseResp buildParamIncompleteResp(BaseResp br)
	{
		br.setStatus(ConstantInfo.RSC_PARAM_INCOMPLETE);
		br.setInfo(ConstantInfo.RSS_PARAM_INCOMPLETE);
		return br;
	}
	
	public BaseResp buildInnerErrorResp(BaseResp br)
	{
		br.setStatus(ConstantInfo.RSC_INNER_ERROR);
		br.setInfo(ConstantInfo.RSS_INNER_ERROR);
		return br;
	}
	
	public BaseResp buildSuccessResp(BaseResp br)
	{
		br.setStatus(ConstantInfo.RSC_SUCCESS);
		br.setInfo(ConstantInfo.RSS_SUCCESS);
		return br;
	}
	
	public BaseResp buildOtherResp(BaseResp br)
	{
		br.setStatus(ConstantInfo.RSC_OTHER);
		br.setInfo("未知原因");
		return br;
	}
	
	public String generateToken(Object obj)
	{
		int hash = obj.hashCode();
		String timestamp = getTimeString();
		return MD5Util.MD5Encode(timestamp+hash, "UTF-8");
	}
	
	/***
	 * 20位唯一订单号
	 * 
	 * @return
	 */
	public String getOutTradeNo20()
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS",Locale.getDefault());
		Date date = new Date();
		String key = format.format(date);

		Random r = new Random();
		key = key + String.format("%03d", r.nextInt(1000));
		return key;
	}
	
	private SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDDhhmmssSSS");
	public String getTimeString()
	{
		return sdf.format(new Date());
	}
}
